import tkinter
import Operation
from tkinter import *
from tkinter.filedialog import *
import json

#  开头
def Action(win):
    font_show = ('楷体', 20)
    label_show = tkinter.Label(win, text='Welcome to 学生信息操作系统！', font=font_show)
    label_show.place(x=200,y = 30)

#  菜单提示栏
def menu_main(win):

    # 菜单提示栏
    menu_main = tkinter.Menu(win)

    # menu_operation = tkinter.Menu(menu_main)
    # menu_find = tkinter.Menu(menu_main)


    list_operation = ['select-<1>','select_all-<2>','insert-<3>','remove-<4>','change-<5>']
    for item in list_operation:
        menu_main.add_command(label = item)


    # mene 下拉
    # for item in ['select-<1>','select_all-<2>']:
    #     menu_find.add_command(label = item)
    # menu_main.add_cascade(label = '查看',menu = menu_find)
    # # menu_operation 下拉栏
    # for item in ['insert-<3>','remove-<4>','change-<5>']:
    #     menu_operation.add_command(label = item)
    # menu_main.add_cascade(label = '编辑',menu = menu_operation)

    win['menu'] = menu_main

    #  -----------  操作流程 -------------

#  指令标签
def label_code(win):
    font_code = ('楷体',15)
    label_code = tkinter.Label(win,text = '请输入指令:',font=font_code )
    label_code.place(x=50,y=90)

# 滚动条列表事件触发
def scroball(reader,text = '学号：{}，信息：{}'):
    # 滚动条事件触发
    def print_item(event):
        print(mylist.get(mylist.curselection()))
    root = tkinter.Tk()
    mylist = Listbox(root,font = '楷体',width=80,height=20)
    mylist.bind('<ButtonRelease-1>',print_item)

    for line in reader:
        mylist.insert(END,text.format(line,reader[line]))

    mylist.pack()
    scrollbar = Scrollbar(root)
    scrollbar.pack(side = RIGHT,fill=Y)
    scrollbar.config(command = mylist.yview)
    mylist.configure(yscrollcommand = scrollbar.set)
    root.mainloop()


#                                        choose学号 --》 指令1 的 行为标签
def sid_button_information(win,record):
    font_code = ('楷体', 15)
    label_input = tkinter.Label(win, text='请输入学号：', font=font_code)
    label_input.place(x=50, y=370)
    entry_input = tkinter.Entry(win, width=20)
    entry_input.place(x=100, y=400)

    def judge1():
        entry_sid = Operation.get_text(entry_input)
        reader = record.reader_students_information()
        if entry_sid not in reader:
            font_tishi = ('楷体', 15)
            label_tishi = tkinter.Label(win, text='无此学生', font=font_tishi, fg='red', width=80)
            label_tishi.place(x=10, y=440)
        else:
            font_tishi = ('楷体', 13)
            button_text = reader[entry_sid]
            button_text1 = '学号：{}，信息：{}'.format(entry_sid, button_text)
            label_information = tkinter.Label(win, text=button_text1, font=font_tishi,width=80)
            label_information.place(x=10, y=440)

    true_button = tkinter.Button(win, text='确定', command=judge1)
    true_button.place(x=260, y=400)

#                                       choose学号 --》 指令 - 2 -- 进行 -- select_all ，并进行相应操作
def sid_panduan2(record):
    reader = record.reader_students_information()
    scroball(reader)

#                                       choose学号 --》 指令 3  的 行为标签  增加
def add_sid(win,record,clas_record_information):
    font=('楷体',15)
    sid_add_label = tkinter.Label(win,text = '请输入学号：',font = font)
    sid_add_label.place(x=330,y=100)
    sid_add_entry = tkinter.Entry(win,width=20)
    sid_add_entry.place(x=380,y=130)


    clas_add_label = tkinter.Label(win,text = '请输入班级：',font=font)
    clas_add_label.place(x=330,y=160)
    clas_add_entry = tkinter.Entry(win,width=20)
    clas_add_entry.place(x=380,y=190)

    name_add_label = tkinter.Label(win,text = '请输入姓名：',font=font)
    name_add_label.place(x=330,y=220)
    name_add_entry = tkinter.Entry(win,width=20)
    name_add_entry.place(x=380,y=250)

    age_add_label = tkinter.Label(win,text = '请输入年龄：',font=font)
    age_add_label.place(x=330,y=280)
    age_add_entry = tkinter.Entry(win,width=20)
    age_add_entry.place(x=380,y=310)

    gender_add_label = tkinter.Label(win,text = '请输入性别：',font=font)
    gender_add_label.place(x=330,y=340)
    gender_add_entry = tkinter.Entry(win,width=20)
    gender_add_entry.place(x=380,y=370)

    def tishi():

        sid = Operation.get_text(sid_add_entry)
        clas = Operation.get_text(clas_add_entry)
        name = Operation.get_text(name_add_entry)
        age = Operation.get_text(age_add_entry)
        gender = Operation.get_text(gender_add_entry)


        with open(r'D:\students_information.txt','r',encoding='utf8') as  file:
            reader = file.read()

            if reader == '':
                if sid == ''or clas == '' or name == '' or age == '' or gender == '':
                    add_true_label = tkinter.Label(win,text = '不能为空！',font = ('楷体',15),fg = 'red')
                    add_true_label.place(x=380,y=400)

                else:
                    record.record(sid, clas, name, age, gender)
                    clas_record_information.record(sid,clas,name,age,gender)

                    add_true_label = tkinter.Label(win, text='添加成功!', font=('楷体', 15), fg='green')
                    add_true_label.place(x=380, y=400)

            elif reader != '':
                reader_sid_information = record.reader_students_information()  # 实例方法


                if sid == ''or clas == '' or name == '' or age == '' or gender == '':
                    add_true_label = tkinter.Label(win,text = '不能为空！',font = ('楷体',15),fg = 'red',width = 10)
                    add_true_label.place(x=380,y=400)

                elif sid in reader_sid_information:
                    add_true_label = tkinter.Label(win,text = '已被注册！',font = ('楷体',15),fg = 'red',width=10)
                    add_true_label.place(x=380,y=400)

                else:

                    record.record(sid, clas, name, age, gender)
                    clas_record_information.record(sid, clas, name, age, gender)

                    add_true_label = tkinter.Label(win,text = '添加成功',font = ('楷体',15),fg = 'green')
                    add_true_label.place(x=380,y=400)

    add_true_label = tkinter.Button(win,text='确定',command = tishi)
    add_true_label.place(x=500,y=400)

#                                       choose学号 --》 指令 4 的 行为 标签 删除
def rem_sid(win,record):
    font = ('楷体', 15)
    sid_rem_label = tkinter.Label(win, text='请输入学号：', font=font)
    sid_rem_label.place(x=600, y=100)
    sid_rem_entry = tkinter.Entry(win, width=20)
    sid_rem_entry.place(x=650, y=130)

    def tishi():
        sid_entry = Operation.get_text(sid_rem_entry) # 输入的学号

        reader = record.reader_students_information()  # 实例方法 读取文件内容-dict

        if sid_entry not in reader:
            sid_rem_label = tkinter.Label(win, text='无此学号!',fg='red', font=font)
            sid_rem_label.place(x=650, y=160)
        else:
            record.rem_students_information(sid_entry) # 实例方法 删除相应学号 key
            sid_rem_label = tkinter.Label(win, text='删除成功！', fg = 'green',font=font)
            sid_rem_label.place(x=650, y=160)

    rem_true_label = tkinter.Button(win,text='确定',command = tishi)
    rem_true_label.place(x=800,y=130)

#                                       choose 学号 --》指令 5 的 行为 标签 修改
def change_sid(win,record):
    font = ('楷体', 15)
    sid_change_label = tkinter.Label(win, text='请输入学生学号：', font=font)
    sid_change_label.place(x=900, y=100)
    sid_change_entry= tkinter.Entry(win, width=20)
    sid_change_entry.place(x=940, y=130)

    sid_change_label2 = tkinter.Label(win, text='请输入学生要换的班级：', font=font)
    sid_change_label2.place(x=900, y=160)
    sid_change_entry2 = tkinter.Entry(win, width=20)
    sid_change_entry2.place(x=940, y=190)

    def tishi():
        sid = Operation.get_text(sid_change_entry)
        clas = Operation.get_text(sid_change_entry2)

        reader = record.reader_students_information()
        if sid not in reader:
            change_label = tkinter.Label(win, text='无此学号!',fg='red', font=font)
            change_label.place(x=940, y=220)

        else:
            record.change(sid, clas)
            change_label = tkinter.Label(win, text='修改成功!',fg='green', font=font)
            change_label.place(x=940, y=220)

    look_button = tkinter.Button(win, text='确定',command = tishi )
    look_button.place(x=1100, y=250)


def other_code(win):
    label_other_code = tkinter.Label(win, text='无此指令！', font=('楷体', 15),fg = 'red')
    label_other_code.place(x=110, y=230)


# choose 班级 --》 指令1 的 行为标签
def class_button_information(win,clas_record_information):
    font_code = ('楷体', 15)
    label_input = tkinter.Label(win, text='请输入班级：', font=font_code)
    label_input.place(x=50, y=370)
    entry_input = tkinter.Entry(win, width=20)
    entry_input.place(x=100, y=400)

    def judge1():
        entry_sid = Operation.get_text(entry_input)

        reader = clas_record_information.reader_sorted_information() # 实例方法

        if entry_sid not in reader:
            font_tishi = ('楷体', 15)
            label_tishi = tkinter.Label(win, text='无此班级', font=font_tishi, fg='red', width=10)
            label_tishi.place(x=100, y=410)
        else:
            font_tishi = ('楷体', 13)
            button_text = reader[entry_sid]
            button_text1 = '班级：{}，信息：{}'.format(entry_sid, button_text)
            button_information = tkinter.Button(win, text=button_text1, font=font_tishi, state='disabled')
            button_information.place(x=100, y=410)

    true_button = tkinter.Button(win, text='确定', command=judge1)
    true_button.place(x=260, y=400)

# choose 班级 --》 指令 2 的行为标签
def class_panduan2(clas_record_information):
    reader = clas_record_information.reader_sorted_information()  # 实例方法
    scroball(reader,text = '班级：{}，信息：{}')

#                                       choose 班级--》 指令 3  的 行为标签  增加
def add_clas(win,clas_record_information):
    font=('楷体',15)
    sid_add_label = tkinter.Label(win,text = '请输入学号：',font = font)
    sid_add_label.place(x=330,y=100)
    sid_add_entry = tkinter.Entry(win,width=20)
    sid_add_entry.place(x=380,y=130)


    clas_add_label = tkinter.Label(win,text = '请输入班级：',font=font)
    clas_add_label.place(x=330,y=160)
    clas_add_entry = tkinter.Entry(win,width=20)
    clas_add_entry.place(x=380,y=190)

    name_add_label = tkinter.Label(win,text = '请输入姓名：',font=font)
    name_add_label.place(x=330,y=220)
    name_add_entry = tkinter.Entry(win,width=20)
    name_add_entry.place(x=380,y=250)

    age_add_label = tkinter.Label(win,text = '请输入年龄：',font=font)
    age_add_label.place(x=330,y=280)
    age_add_entry = tkinter.Entry(win,width=20)
    age_add_entry.place(x=380,y=310)

    gender_add_label = tkinter.Label(win,text = '请输入性别：',font=font)
    gender_add_label.place(x=330,y=340)
    gender_add_entry = tkinter.Entry(win,width=20)
    gender_add_entry.place(x=380,y=370)

    def tishi():

        sid = Operation.get_text(sid_add_entry)
        clas = Operation.get_text(clas_add_entry)
        name = Operation.get_text(name_add_entry)
        age = Operation.get_text(age_add_entry)
        gender = Operation.get_text(gender_add_entry)

        reader_clas_information = clas_record_information.reader_sorted_information()  # 实例方法

        if reader_clas_information == '':
            if sid == ''or clas == '' or name == '' or age == '' or gender == '':
                add_true_label = tkinter.Label(win,text = '不能为空！',font = ('楷体',15),fg = 'red')
                add_true_label.place(x=380,y=400)

            else:
                clas_record_information.record(sid,clas,name,age,gender)  # 实例方法 加载到文件

                add_true_label = tkinter.Label(win, text='添加成功!', font=('楷体', 15), fg='green')
                add_true_label.place(x=380, y=400)

        else:

            list_information1 = []
            list_information2 = []
            list_sid = []
            for i in reader_clas_information:
                list_information1.append(reader_clas_information[i])

            for listi in list_information1:
                list_type1 = ["<class 'list'>", "<class 'object'>"]
                list_type2 = ["<class 'dict'>", "<class 'object'>"]
                if str(type(listi)) in list_type1:
                    for i in listi:
                        list_information2.append(i)
                elif str(type(listi)) in list_type2:
                    list_information2.append(listi)

            for id in list_information2:
                list_sid.append(id['学号'])


            if sid == ''or clas == '' or name == '' or age == '' or gender == '':
                add_true_label = tkinter.Label(win,text = '不能为空！',font = ('楷体',15),fg = 'red',width = 10)
                add_true_label.place(x=380,y=400)

            else:
                if sid  in list_sid:
                    print('-----------')
                    add_true_label = tkinter.Label(win,text = '已被注册！',font = ('楷体',15),fg = 'red',width=10)
                    add_true_label.place(x=380,y=400)

                else:
                    clas_record_information.record(sid, clas, name, age, gender)  # 实例方法

                    add_true_label = tkinter.Label(win, text='添加成功', font=('楷体', 15), fg='green')
                    add_true_label.place(x=380, y=400)




    add_true_label = tkinter.Button(win,text='确定',command = tishi)
    add_true_label.place(x=500,y=400)

#                                       choose 班级  --》 指令 4 的 行为 标签 删除
def rem_clas(win,clas_record_information):
    font = ('楷体', 15)
    clas_rem_label = tkinter.Label(win, text='请输入班级：', font=font)
    clas_rem_label.place(x=600, y=200)
    clas_rem_entry1 = tkinter.Entry(win, width=20)
    clas_rem_entry1.place(x=650, y=230)

    clas_rem_sid = tkinter.Label(win, text='请输入要删除的学生学号：', font=font)
    clas_rem_sid.place(x=600, y=260)
    clas_rem_entry2 = tkinter.Entry(win, width=20)
    clas_rem_entry2.place(x=650, y=290)

    def tishi():
        clas_entry1 = Operation.get_text(clas_rem_entry1)   # 输入的班级
        clas_entry2 = Operation.get_text(clas_rem_entry2)   # 输入的学号

        reader = clas_record_information.reader_sorted_information()  # 实例方法 读取文件内容-dict

        if clas_entry1 not in reader:
            sid_rem_label = tkinter.Label(win, text='无此班级!',fg='red', font=font)
            sid_rem_label.place(x=650, y=160)

        elif clas_entry1 in reader:
            res = clas_record_information.rem_sorted_information(clas_entry1, clas_entry2)  # 实例方法 删除相应学号 所在的字典
            if res == 'false':
                sid_rem_label = tkinter.Label(win, text='输入错误！', fg = 'red',font=font)
                sid_rem_label.place(x=650, y=160)

            else:
                with open(r'D:\sorted_students_information.txt', 'w', encoding='utf8') as f:
                    for i in reader[clas_entry1]:
                        reader[clas_entry1].remove(i)
                        files = json.dumps(reader)
                        f.write(files)
                        sid_rem_label = tkinter.Label(win, text='删除成功！', fg = 'green',font=font)
                        sid_rem_label.place(x=650, y=160)


    rem_true_label = tkinter.Button(win,text='确定',command = tishi)
    rem_true_label.place(x=800,y=330)

#                                      choose  班级 --》  指令 5   行为  改变
def change_clas(win,clas_record_information):
    font = ('楷体', 15)
    sid_change_label = tkinter.Label(win, text='请输入学生学号：', font=font)
    sid_change_label.place(x=900, y=100)
    sid_change_entry= tkinter.Entry(win, width=20)
    sid_change_entry.place(x=940, y=130)

    clas_change_label2 = tkinter.Label(win, text='请输入学生原来的班级：', font=font)
    clas_change_label2.place(x=900, y=160)
    clas_change_entry2 = tkinter.Entry(win, width=20)
    clas_change_entry2.place(x=940, y=190)

    clas_change_label3 = tkinter.Label(win, text='请输入学生要换的班级：', font=font)
    clas_change_label3.place(x=900,y=220)
    clas_change_entry3 = tkinter.Entry(win,width=20)
    clas_change_entry3.place(x=940,y=250)


    def tishi():
        sid = Operation.get_text(sid_change_entry)
        source_clas = Operation.get_text(clas_change_entry2)
        new_clas = Operation.get_text(clas_change_entry3)

        reader = clas_record_information.reader_sorted_information()

        if new_clas not in reader:
            change_label = tkinter.Label(win, text='无此班级!',fg='red', font=font)
            change_label.place(x=940, y=220)

        else:
            clas_record_information.change(sid, source_clas,new_clas)
            change_label = tkinter.Label(win, text='修改成功!',fg='green', font=font)
            change_label.place(x=940, y=300)

    look_button = tkinter.Button(win, text='确定',command = tishi )
    look_button.place(x=1100, y=250)

def no_choose(win):
    label_other_code = tkinter.Label(win, text='请选择！', font=('楷体', 15),fg = 'red')
    label_other_code.place(x=110, y=230)


# 判断指令 - 1 -- 进行 -- select
def choose(entry_code,r,win):
    get_choose = r.get()
    entry_text = Operation.get_text(entry_code)

    record = Operation.Record_students_information()
    clas_record_information = Operation.Sorted_information()  # 实例化


    if get_choose=='1':     #  学号

        if entry_text == '1':
            sid_button_information(win,record)

        elif entry_text == '2':
            sid_panduan2(record)

        elif entry_text == '3':   #   增
            add_sid(win,record,clas_record_information)

        elif entry_text == '4':  #   删
            rem_sid(win,record)

        elif entry_text =='5':  #   改
            change_sid(win,record)
        else:
            other_code(win)

    elif get_choose =='2':       #  班级

        if entry_text =='1':
            class_button_information(win,clas_record_information)

        elif entry_text == '2':
            class_panduan2(clas_record_information)

        elif entry_text == '3':
            add_clas(win,clas_record_information)

        elif entry_text == '4':
            rem_clas(win,clas_record_information)

        elif entry_text == '5':
            change_clas(win,clas_record_information)

        else:
            other_code(win)

    else:
        no_choose(win)


def opertion():

    window = tkinter.Toplevel()
    window.title(' Students Information !')
    window.geometry('800x600')

    # 开头
    Action(window)

    # 菜单提示栏
    menu_main(window)

    #  -----------  操作流程 -------------

    # 指令标签栏
    label_code(window)

    # entry栏
    entry_code = tkinter.Entry(window,width = 20)
    entry_code.place(x=100,y=120)

    # 复选栏
    label_Checkbutton = tkinter.Label(window, text='Please  choose', font=('Arial', 12))
    label_Checkbutton.place(x=130, y=160)
    r = tkinter.StringVar()
    r.set(0)
    choose_admin = tkinter.Radiobutton(window, text='学号', variable=r, value='1')
    choose_admin.place(x=110, y=190)
    choose_students = tkinter.Radiobutton(window, text='班级', variable=r, value='2')
    choose_students.place(x=230, y=190)

    #  判断指令 并执行操作    panduan -（  entry_code ）
    def judge1():
        # entry_text = Operation.get_text(entry_code)
        choose(entry_code,r,window)

    true_button = tkinter.Button(window,text = '确定',command = judge1)
    true_button.place(x=250,y=120)
    # window.mainloop()

